package ar.edu.utn.frgp.labtec.actions.cliente;

import java.util.ArrayList;
import java.util.List;

import ar.edu.utn.frgp.labtec.constants.WebConstants;
import ar.edu.utn.frgp.labtec.entities.Cliente;
import ar.edu.utn.frgp.labtec.service.GestionClienteService;

public class ListarClientesAction extends ClienteAction {
	
	private String razonSocialAbuscar;
	private String cuitAbuscar; 
	private String responsableAbuscar; 

	private static final long serialVersionUID = 1L;

	public String execute(){
		clienteNoEncontrado = "false";
		if(WebConstants.GESTION_LIST.equals(typeModif)){
			listClientes = listarClientes();
		}else if(WebConstants.GESTION_FILTER.equals(typeModif)){
			listClientes = listarClientesFiltrados();
			if(listClientes== null || listClientes.isEmpty()){
			  clienteNoEncontrado = "true";	
			  listClientes = listarClientes();
			}
			if(returnAction.equals(WebConstants.GESTION_MODIFICACION)){
				return WebConstants.SUCCESS_UPDATE;
			}else if (returnAction.equals(WebConstants.GESTION_CONSULTA)){
				return WebConstants.SUCCESS_CONSULTA;
			}else if (returnAction.equals(WebConstants.GESTION_DELETE)){
				return WebConstants.SUCCESS_DELETE;
			} else if (returnAction.equals(WebConstants.CLIENTE_REVISION)){
				return WebConstants.SUCCESS_REVISION;
			}
				
		}else if(WebConstants.GESTION_NEW.equals(typeModif)){
			listClientes = listarCliente(codCliente);
		}else if(WebConstants.GESTION_UPDATE.equals(typeModif)){
			listClientes = listarCliente(codCliente);
		}else if(WebConstants.GESTION_DELETE.equals(typeModif)){
			listClientes = listarClientes();
		}
		
		return SUCCESS;
	}

	private List<Cliente> listarCliente(Integer codCliente) {
		GestionClienteService clienteService = (GestionClienteService) getContext().getBean("gestionClienteService");
		List<Cliente> registro = new ArrayList<Cliente>();
		registro = clienteService.getRegistro(codCliente);
		registro = formatearDatos(registro);
		return registro;
	}

	private List<Cliente> listarClientesFiltrados() {
		GestionClienteService clienteService = (GestionClienteService) getContext().getBean("gestionClienteService");
		List<Cliente> lista = new ArrayList<Cliente>();
		lista = clienteService.getClientesFiltrados(razonSocialAbuscar, cuitAbuscar, responsableAbuscar);
		lista = formatearDatos(lista);
		return lista;
	}

	private List<Cliente> listarClientes() {
		GestionClienteService clienteService = (GestionClienteService) getContext().getBean("gestionClienteService");
		
		List<Cliente> lista = new ArrayList<Cliente>();
		lista = clienteService.getClientes();
		lista = formatearDatos(lista);
		return lista;
	}

	public String getRazonSocialAbuscar() {
		return razonSocialAbuscar;
	}

	public void setRazonSocialAbuscar(String razonSocialAbuscar) {
		this.razonSocialAbuscar = razonSocialAbuscar;
	}

	public String getCuitAbuscar() {
		return cuitAbuscar;
	}

	public void setCuitAbuscar(String cuitAbuscar) {
		this.cuitAbuscar = cuitAbuscar;
	}

	public String getResponsableAbuscar() {
		return responsableAbuscar;
	}

	public void setResponsableAbuscar(String responsableAbuscar) {
		this.responsableAbuscar = responsableAbuscar;
	}
	
	
	
}
